package junk;

import org.apache.commons.io.FileUtils;
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testng.annotations.Test;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.Date;

import static org.testng.Assert.assertEquals;

public class webdriver_do {

    public WebDriver wd;


    @Test

    public void init_webdriver() throws IOException {

        String driverPath = new File("./").getCanonicalPath() + "/src/test/java/driver/chromedriver.exe";
        String driverName = "webdriver.chrome.driver";


        System.setProperty(driverName, driverPath);
/*

        DesiredCapabilities desiredCapabilities;
//        判断要打开的浏览器
        ChromeOptions chromeOptions = new ChromeOptions();
        // chromeOptions.addArguments("--headless");
        chromeOptions.addArguments("--headless","--disable-gpu","--window-size=1290,1080");

        desiredCapabilities = DesiredCapabilities.chrome();
        desiredCapabilities.setCapability("chromeOptions", chromeOptions);

//        拼接处要执行脚本的node 节点地址
        // String url = nodeURL + "/wd/hub";
        String url = "http://192.168.1.7:4444/wd/hub";
        wd = new RemoteWebDriver(new URL(url), desiredCapabilities);
*/
        wd=new ChromeDriver();

        wd.manage().window().maximize();
      //  wd.get("http://www.baidu.com");


        System.out.println("-----webdriver初始化完毕！！！----\n");

        /*截图*/
       // takeScreenshot(wd);


       // test_wd();

    }


    public void PlaySpeed(){
        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }



    public void test_wd() {
        try {
            Thread.sleep(3600);
            wd.close();
            wd.quit();
            System.out.println("----close browser!!!-----");

        } catch (InterruptedException e) {
            e.printStackTrace();
        }
         System.out.println("wd ---ok");
    }

    /**
     * 是否存在某个节点
     *
     * @param by
     * @param wd
     * @return
     */
    public boolean isPresentEle(By by, WebDriver wd) {
        try {
            wd.findElement(by);
            return true;
        } catch (Exception e) {
            return false;
        }
    }


    /**
     * 获取当前时间，并转换为字符串
     * @return
     */
    private static String Date2String(){
        Format format = new SimpleDateFormat("yyyyMMddHHmmss");
        return format.format(new Date());
    }

    /**
     * 自动截屏
     * @param wd
     */
    public  void takeScreenshot(WebDriver wd){
        try {
            //获取截图file
            File srcFile= ((TakesScreenshot)wd).getScreenshotAs(OutputType.FILE);


            //新文件
            File targeFile = new File("pic/" + Date2String()+".png");

            //将图片移动到指定位置
            FileUtils.copyFile(srcFile, targeFile);
        }  catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取断言失败的原因、信息和截屏
     * @param one
     * @param two
     * @param wd
     * @throws IOException
     * @throws InterruptedException
     */
    public  void assertEqu(String one,String two,WebDriver wd) throws InterruptedException, IOException{
        try{
            Thread.sleep(3000);
            takeScreenshot(wd);
            assertEquals(one, two);

        }catch(Exception e){
            System.out.println(e.getCause());
            System.out.println(e.getMessage());
        }
    }




}
		
	
	

	
